package top.rainf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.rainf.service.IChatRecordService;
import top.rainf.support.Result;
import top.rainf.domain.vo.ChatRecord;

import java.util.List;

/**
 * @author 星环
 * @date Wed May 10 21:04:41 CST 2023
 * @description 聊天记录的控制器类 由本人的生成器自动生成~
 */
@RestController
@RequestMapping("/chatRecord")
public class ChatRecordController {
    @Autowired
    IChatRecordService chatRecordService;
    /**
    * 获取全部聊天记录的列表
    * @return 返回是否获取成功~
    */
    @RequestMapping("/list")
    public Result list(){
        List<ChatRecord> list = chatRecordService.getChatRecordList();
        if(list == null){
            return Result.fail();
        }
        return Result.success().addObj("list",list);
    }

    /**
     * 获取用户与某个好友对话的初始聊天记录，应该最多显示100条的
     * @param userId
     * @param friendId
     * @return
     */
    @RequestMapping("/listByUserId")
    public Result listByUserId(int userId,int friendId){
        System.out.println(userId+" " + friendId);
        List<ChatRecord> recordList = chatRecordService.getListByUserId(userId,friendId);
        return Result.success().addObj("list",recordList);
    }

    /**
     * 正在聊天时，查看新收到的消息，打开对话框时，每几秒请求一次
     * 因为数据库用checked标记了，只需要选取对方发过来没看过的信息就行
     * @param userId
     * @param friendId
     * @return
     */
    @RequestMapping("/getNewMessage")
    public Result getNewMessage(int userId,int friendId){
        List<ChatRecord> list = chatRecordService.getNewMessage(userId,friendId);
        return Result.success().addObj("list",list);
    }

    /**
     * 发信息
     * @param userId
     * @param friendId
     * @param msg
     * @return
     */
    @RequestMapping("/insertNewMsg")
    public Result insertNewMsg(int userId,int friendId,String msg){
        chatRecordService.sendMsg(userId,friendId,msg);
        return Result.success();
    }

    /**
    * 根据id获取一个聊天记录
    * @return 返回是否获取成功~
    */
    @RequestMapping("/one")
    public Result getOneChatRecord(int id){
        ChatRecord chatRecord = chatRecordService.getOneById(id);
        if(chatRecord == null){
                    return Result.fail();
        }
        return Result.success().addObj("data",chatRecord);
    }
    /**
    * 根据id删除一个聊天记录
    ---------------------------
    * @param id 要删除的聊天记录的id
    * @return 返回是否获取成功~
    */
    @RequestMapping("/deleteById")
    public Result deleteById(int id){
        boolean suc = chatRecordService.deleteOneById(id);
        return suc ? Result.success() : Result.fail();
    }

    @RequestMapping("/deleteByIds")
    public Result deleteByIds(List<Integer> idList){
        boolean suc = chatRecordService.deleteAll(idList);
        return suc ? Result.success() : Result.fail();
    }

    @RequestMapping("update")
    public Result update(ChatRecord chatRecord){
        boolean suc = chatRecordService.updateById(chatRecord);
        return suc ? Result.success() : Result.fail();
    }
}
